package org.jeecg.modules.wms.inorder.controller;

import java.util.Arrays;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wms.wmstask.entity.WmsTasks;
import org.jeecg.modules.wms.wmstask.entity.WmsTasksRecords;
import org.jeecg.modules.wms.wmstask.service.IWmsTasksRecordsService;
import org.jeecg.modules.wms.wmstask.service.IWmsTasksService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 任务表
 * @Author: jeecg-boot
 * @Date: 2025-10-28
 * @Version: V1.0
 */
@Tag(name = "任务表")
@RestController
@RequestMapping("/inorder/receiveTasks")
@Slf4j
public class ReceiveTasksController extends JeecgController<WmsTasks, IWmsTasksService> {
    @Autowired
    private IWmsTasksService wmsTasksService;
    @PostMapping("/addRecords")
    public Result<String> addRecords(@RequestBody WmsTasksRecords records) {
        String id = records.getId();
        records.setTaskId(id);
        records.setId(null);
        wmsTasksService.receive(records);
        return Result.OK("添加成功！");
    }

    /**
     * 分页列表查询
     *
     * @param wmsTasks
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<IPage<WmsTasks>> queryPageList(WmsTasks wmsTasks,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        QueryWrapper<WmsTasks> queryWrapper = QueryGenerator.initQueryWrapper(wmsTasks, req.getParameterMap());
        Page<WmsTasks> page = new Page<WmsTasks>(pageNo, pageSize);

//		IPage<WmsTasks> pageList = wmsTasksService.page(page, queryWrapper);
        IPage<WmsTasks> pageList = wmsTasksService.pagePlus(page, wmsTasks);
        return Result.OK(pageList);
    }

    /**
     * 添加
     * * @return
     */
    @AutoLog(value = "任务表-添加")
    @Operation(summary = "任务表-添加")
    @RequiresPermissions("inorder:wms_stock_in_orders:add")
    @PostMapping(value = "/add")
    public Result<String> add(String orderIds, String operator) {
        String[] ids = orderIds.split(",");
        for (String id : ids) {
            wmsTasksService.createReceiveTasks(id, operator);
        }
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wmsTasks
     * @return
     */
    @AutoLog(value = "任务表-编辑")
    @Operation(summary = "任务表-编辑")
    @RequiresPermissions("wmstask:wms_tasks:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmsTasks wmsTasks) {
        wmsTasksService.updateById(wmsTasks);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "任务表-通过id删除")
    @Operation(summary = "任务表-通过id删除")
    @RequiresPermissions("wmstask:wms_tasks:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        wmsTasksService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "任务表-批量删除")
    @Operation(summary = "任务表-批量删除")
    @RequiresPermissions("wmstask:wms_tasks:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmsTasksService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "任务表-通过id查询")
    @Operation(summary = "任务表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsTasks> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsTasks wmsTasks = wmsTasksService.getById(id);
        if (wmsTasks == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsTasks);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsTasks
     */
    @RequiresPermissions("wmstask:wms_tasks:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsTasks wmsTasks) {
        return super.exportXls(request, wmsTasks, WmsTasks.class, "任务表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("wmstask:wms_tasks:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsTasks.class);
    }

    @Autowired
    private IWmsTasksRecordsService wmsTasksRecordsService;

    /**
     * 分页列表查询
     *
     * @param wmsTasksRecords
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "任务执行记录表-分页列表查询")
    @Operation(summary = "任务执行记录表-分页列表查询")
    @GetMapping(value = "/records")
    public Result<IPage<WmsTasksRecords>> queryPageList(WmsTasksRecords wmsTasksRecords,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<WmsTasksRecords> queryWrapper = QueryGenerator.initQueryWrapper(wmsTasksRecords, req.getParameterMap());
        Page<WmsTasksRecords> page = new Page<WmsTasksRecords>(pageNo, pageSize);
//         IPage<WmsTasksRecords> pageList = wmsTasksRecordsService.page(page, queryWrapper);
        IPage<WmsTasksRecords> pageList = wmsTasksRecordsService.pageLists(page, wmsTasksRecords);
        return Result.OK(pageList);
    }

}
